#require jQuery

#
# Define
#
WEBSOCK_URL = 'ws://127.0.0.1:50080/'
#WEBSOCK_URL = 'ws://192.168.0.3:3000/'

g_socket = null

#
# Streaming data object
#
class StreamingPacket
  buffer_view: null
  uid_buf: null
  sid_buf: null
  len_buf: null
  pcm_buff: null
  HEADER_SIZE: (4 + 4 + 4)

  #
  # create empty buffer 
  #
  create_empty_packet:(pcm_buff_len)->
    @buffer_view = new ArrayBuffer(@HEADER_SIZE + pcm_buff_len * 4)
    @map_header()
    @pcm_buff = new Float32Array(@buffer_view, @HEADER_SIZE, pcm_buff_len)

  #
  # map header buffer views 
  #
  map_header:->
    offset=0
    @uid_buf = new Uint32Array(@buffer_view, offset, 1)
    offset += 4
    @sid_buf = new Uint32Array(@buffer_view, offset, 1)
    offset += 4
    @len_buf = new Uint32Array(@buffer_view, offset, 1)

  #
  # returns data(ArrayBuffer) of packet
  #
  to_array_buffer:(uid, sid, pcm_data)->
    console.log pcm_data.length
    console.log @pcm_buff.length

    if pcm_data.length != @pcm_buff.length then return null

    @uid_buf[0] = uid
    @sid_buf[0] = sid
    @len_buf[0] = pcm_data.length
    for i in [0..pcm_data.length]
      @pcm_buff[i] = pcm_data[i]

    return @buffer_view

  #
  # packet binary blob data to map buffer views
  #
  from_arraybuffer:(data)->
    if data instanceof ArrayBuffer
      @buffer_view = data
      @map_header()
      @pcm_buff = new Float32Array(@buffer_view, @HEADER_SIZE, @get_pcmbuff_len())
    else 
      console.log 'unknown data type'

  get_uid:()->
    if @uid_buf?
      return @uid_buf[0]
    else
      return null

  get_sid:()->
    if @sid_buf?
      return @sid_buf[0]
    else
      return null

  get_pcmbuff_len:->
    if @len_buf?
      @len_buf[0]
    else
      return 0

#------------------
# On ready
#------------------
$ ->
  $('#btn-connect').click =>
    console.log("test");
    if window.MozWebSocket?
      g_socket = new window.MozWebSocket(WEBSOCK_URL);
    else
      g_socket = new window.WebSocket(WEBSOCK_URL);

    g_socket.binaryType = 'arraybuffer'

    #
    # Socket : on connection established
    #
    g_socket.onopen = (e)->
      console.log 'connected'
      console.log e

    #
    # Socket : on received message from server
    #
    g_socket.onmessage = (message)->
      console.log "onmessage : #{typeof message.data}"
      #view = new DataView(message.data)
      #array = view.getFloat32(0)
      #array = new Float32Array(message.data)

      packet = new StreamingPacket()
      packet.from_arraybuffer(message.data)
      $('#console').prepend('<p>' + packet.pcm_buff + '</p>')
      for val in packet.pcm_buff
        console.log val

    #
    # Socket : on closed
    #
    g_socket.onclose  = ->
      console.log 'onclose'

  #
  # User interaction handler
  #
  $('#btn-send').click =>
    #g_socket.send($('#input').val())
    array = new Float32Array(10)
    for i in [0..10]
      array[i] = i * i / 3

    for val in array
      console.log val

    # Uint8Array? : Type for other 
    # g_socket.send(new Uint8Array(array))

    #ab = new ArrayBuffer((10 * 4));
    #view = new DataView(ab);
    #offset = 0
    #for fval in array
    #  view.setFloat32(offset, fval)
    #  offset += 4;

    # ArrayBuffer : Type for mozilla 
    packet = new StreamingPacket()
    packet.create_empty_packet(array.length)
    data = packet.to_array_buffer(123, 456, array)
    g_socket.send(data)

    $('#input').val('')

  $('#btn-close').click =>
    g_socket.close();
